package cn.xing.managementSystem.service;

import cn.xing.managementSystem.bean.pageable.PageableConverter;
import cn.xing.managementSystem.bean.pageable.PageableRequest;
import cn.xing.managementSystem.bean.pageable.PageableResponse;
import cn.xing.managementSystem.domain.ImageStore;
import cn.xing.managementSystem.domain.Information;
import cn.xing.managementSystem.enums.InformationType;
import cn.xing.managementSystem.repository.InformationRepository;
import cn.xing.managementSystem.utils.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author cuixing
 * @package cn.xing.managementSystem.service
 * @class InformationService
 * @email xing.cui@magicwindow.cn
 * @date 2018/4/25 上午11:14
 * @description
 */

@Service
@Transactional
public class InformationService {

    @Autowired
    private InformationRepository informationRepository;

    @Autowired
    private ImageStoreService imageStoreService;

    @Value("${image.boot.path}")
    private String imageBootPath;


    public Information findById(Long informationId) {
        return informationRepository.findByIdAndAndDeletedIsFalse(informationId);
    }

    public boolean saveInformationAndImageStore(Information information, Long informationImageId, MultipartFile informationImageFile) throws IOException {
        if(Preconditions.isBlank(informationImageId) && Preconditions.isNotBlank(informationImageFile)){
            ImageStore imageStore = imageStoreService.saveImageStore(informationImageFile);
            information.setImageStore(imageStore);
        }
        information = informationRepository.saveAndFlush(information);
        return Preconditions.isNotBlank(information);
    }

    public Information saveInformation(Information information) {
        return informationRepository.saveAndFlush(information);
    }

    public PageableResponse<Information> listPageable(PageableRequest pageableRequest){
        PageableResponse<Information> response = new PageableResponse<>();
        Page<Information> page = informationRepository.findByDeletedIsFalse(PageableConverter.toPageRequest(pageableRequest));
        response.setList(page.getContent());
        response.setTotalCount(page.getTotalElements());
        response.setTotalPages(page.getTotalPages());
        response.setCurPage(pageableRequest.getPageNumber() + 1);
        return response;
    }

    public List<Information> getInformationListByType(InformationType informationType){
        return informationRepository.findByInformationTypeAndDeletedIsFalseOrderByDateCreatedDesc(informationType);
    }


    public Information getLastestInformationByType(InformationType informationType){
        return informationRepository.findFirstByInformationTypeAndDeletedIsFalseOrderByDateCreatedDesc(informationType);
    }
}